package com.haixee.parkdemo.common.response;


import com.haixee.parkdemo.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常工具类
 *
 * @author RUIFENG GUO
 * 2020/3/24 11:22
 */
@Slf4j
public final class ExceptionUtil {

    public static ResResult getExceptionResponse(Exception ex) {
        ResResult commonRes;

        if (ex instanceof BusinessException) {
            BusinessException exception = (BusinessException) ex;
            commonRes = ResUtil.error(exception.getCode(), exception.getMessage());
            commonRes.setStatus(exception.getStatus());
//        } else if (ex instanceof UndeclaredThrowableException &&
//                ((UndeclaredThrowableException) ex).getUndeclaredThrowable() instanceof DubboWrapperBusinessException) {
//            DubboWrapperBusinessException exception = (DubboWrapperBusinessException) ((UndeclaredThrowableException) ex).getUndeclaredThrowable();
//            commonRes = ResUtil.error(exception.getCode(), exception.getMessage());
//            commonRes.setStatus(exception.getStatus());

        } else if (ex instanceof BindException) {
            BindException exception = (BindException) ex;
            String errorString = RequestUtil.processError(exception.getBindingResult());
            commonRes = ResUtil.error(CommonResEnum.BAD_REQUEST, errorString);
            commonRes.setStatus(HttpStatus.BAD_REQUEST);
//        } else if (ex instanceof TokenException) {
//            TokenException exception = (TokenException) ex;
//            JwtStatus stateEnum = exception.getStateEnum();
//            if (JwtStatus.TOKEN_EXPIRED.equals(stateEnum)) {
//                commonRes = ResUtil.error(CommonResEnum.TOKEN_EXPIRED);
//                commonRes.setStatus(HttpStatus.UNAUTHORIZED);
//            } else {
//                commonRes = ResUtil.error(CommonResEnum.PERMISSION_DENIED, "TOKEN 异常");
//                commonRes.setStatus(HttpStatus.UNAUTHORIZED);
//            }
        } else if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
            String errorString = RequestUtil.processError(exception.getBindingResult());
            commonRes = ResUtil.error(CommonResEnum.BAD_REQUEST, errorString);
            commonRes.setStatus(HttpStatus.BAD_REQUEST);
        } else if (ex instanceof HttpMessageNotReadableException) {
            commonRes = ResUtil.error(CommonResEnum.BAD_REQUEST);
            commonRes.setStatus(HttpStatus.BAD_REQUEST);
        } else if (ex instanceof NoHandlerFoundException) {
            commonRes = ResUtil.error(CommonResEnum.NO_HANDLER_FOUND);
            commonRes.setStatus(HttpStatus.NOT_FOUND);
        } else if (ex instanceof HttpMediaTypeNotSupportedException) {
            commonRes = ResUtil.error(CommonResEnum.NO_HANDLER_FOUND, ex.getMessage());
            commonRes.setStatus(HttpStatus.BAD_REQUEST);
        } else if (ex instanceof ServletRequestBindingException) {
            commonRes = ResUtil.error(CommonResEnum.BAD_REQUEST);
            commonRes.setStatus(HttpStatus.BAD_REQUEST);
        } else if (ex instanceof HttpRequestMethodNotSupportedException) {
            commonRes = ResUtil.error(CommonResEnum.NO_HANDLER_FOUND);
            commonRes.setStatus(HttpStatus.NOT_FOUND);
        } else {
            log.error("未知错误", ex);
            commonRes = ResUtil.error(CommonResEnum.UNKNOWN_ERROR);
            commonRes.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return commonRes;
    }
}
